package com.dsunsoft.rtu.service.impl;

import com.dsunsoft.rtu.common.exception.BaseException;
import com.dsunsoft.rtu.common.util.CommonUtils;
import com.dsunsoft.rtu.common.vo.NgSelectVO;
import com.dsunsoft.rtu.core.base.BaseServiceImpl;
import com.dsunsoft.rtu.dao.RtuDeviceMapper;
import com.dsunsoft.rtu.entity.RtuDevice;
import com.dsunsoft.rtu.query.RtuDeviceQuery;
import com.dsunsoft.rtu.service.RtuDeviceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RtuDeviceServiceImpl extends BaseServiceImpl<RtuDeviceMapper, RtuDevice, RtuDeviceQuery> implements RtuDeviceService {


    @Transactional
    @Override
    public boolean insert(RtuDevice entity) {
        int count = mapper.selectCount(entity);
        if (count > 0) {
            throw new BaseException("设备ID已存在!");
        }
        entity.setId(CommonUtils.getUuid());
        return super.insert(entity);
    }

    @Transactional
    @Override
    public boolean update(RtuDevice entity) {
        int count = mapper.selectCount(entity);
        if (count > 0) {
            throw new BaseException("设备ID已存在!");
        }
        return super.update(entity);
    }

    @Override
    public List<NgSelectVO> getSelect(RtuDeviceQuery query) {
        return mapper.getSelect(query);
    }

    @Override
    public List<String> getDeviceIdList() {
        return mapper.selectDeviceIdList();
    }

    @Transactional
    @Override
    public void updateOnlineStatus(String deviceId, Integer onlineStatus) {
        mapper.updateOnlineStatus(deviceId, onlineStatus);
    }
}
